﻿@using System.Text
@using Telerik.Web.Mvc.Extensions
@model dynamic

@{
    IEnumerable<CmsPage> pages = Model.Pages;
    bool renderHeader = Model.UseHeader;
    PageTreeHistory history = PageTreeHistory.Current;
    ResFile = "cms_Pages";
}
           
@functions{  
    IHtmlString MakeIndent(int pageLevel)
    {
        var sb = new System.Text.StringBuilder();

        for (int i = 0; i < pageLevel-1; i++)
        {
            sb.Append("<i class='b-invisible'>&nbsp;&mdash;&nbsp;</i>");    
        }

        return Html.Raw(sb.ToString());
    }
    
    string PageName(CmsPage page)
    {
        return page.IsVisible ?
            (page.IsPublished ? page.PageName : _("{0} (Unpublished)", page.PageName)) :
            _("{0} (Hidden)", page.PageName);
    }
    
    //IEnumerable<int> CheckCurrentClass(CmsPage page, IEnumerable<int> ids = null)
    //{
    //    if (page.ParentPageId.HasValue)
    //    {
    //        var a = ids.ToList();
    //        a.Add(page.ParentPageId.Value);
    //        ids = CheckCurrentClass(page.ParentPage, a.ToArray());
    //    }
        
    //    return ids;
    //}

    List<int> GetAllParentPageIds(CmsPage page, List<int> ids)
    {
        if (page.ParentPageId.HasValue)
        {
            ids.Add(page.ParentPageId.Value);
            ids = GetAllParentPageIds(page.ParentPage, ids);
        }
        return ids;
        //var result = new List<CmsPage>();

        //if (page.ParentPageId.HasValue)
        //{
        //    result.Add(page);
        //    page = GetAllParentPageIds(page.ParentPage);
        //}
        //return result;
    }
}

@(Html.CmsGrid<CmsPage>(pages).Attributes(new { @class = "t-grid" }).Name("PageGrid").RenderHeader(renderHeader)
    .Columns(
        columns =>
        {
            columns.HeaderCssClass("t-grid-header").Bound().Title(_("Page title")).HtmlAttributes(new { @class = "t-header" });
            columns.Bound().Width(60).HtmlAttributes(new { @class = "t-header" });
            columns.Bound().Width(30).HtmlAttributes(new { @class = "t-header" });
            columns.Bound().Title(_("Publication date")).Width(100).HtmlAttributes(new { @class = "t-header" });
            columns.Bound().Title(_("Creator")).HtmlAttributes(new { @class = "t-header" });
            columns.Bound().Title(_("Template")).HtmlAttributes(new { @class = "t-header" });
            columns.Bound().Title(_("Copy")).Width(46).HtmlAttributes(new { @class = "t-header" });
            columns.Bound().Title(_("Delete")).Width(50).HtmlAttributes(new { @class = "t-header t-last-header" });
        })
        .RowCssCondition(
            (p) =>
            {
                var result = new StringBuilder();
                var parentPageIds = GetAllParentPageIds(p, new List<int>());
                if (parentPageIds != null && parentPageIds.Any())
                {
                    foreach (var parentPageId in parentPageIds)
                    {
                        result.AppendFormat("child-page-{0} ", parentPageId);
                    }
                }
                
                if (!p.IsVisible || !p.IsPublished)
                {
                    result.Insert(0 , "t-disabled ");
                }
               
                return string.Format("content-type {0}-type {1}", p.BehaviourPageType.ToString().ToLower(), result);
            })
    .AlternativeRow(
        @<text>
            @if (history.IsOpen(item.Id))
            {
                @Html.Action("getchildren", "cmspage", new { id = item.Id })
            }
        </text>)
    .Rows(
        rows =>
        {
            rows.Bound().RowItemTemplate(
                @<text>
                     @MakeIndent(item.PageLevel)
                     @if (item.ParentPageId.HasValue && (item.ChildrenCount > 0))
                     {
                         <a class="b-icon b-icon_expand @(history.IsOpen(item.Id) ? "b-icon_expand_min" : "b-icon_expand_max")" rel="@item.Id" rev="child-page-" href="@Url.Action("getchildren", "cmspage", new { id = item.Id })"></a>                    
                     }
                     
                     @if(item.Template == null || !item.Template.DefaultRegionId.HasValue)
                     {
                         <a class="page-title" href="@Url.Action("edit", "cmspage", new { id = item.Id })">@PageName(item)</a>
                     }
                     else
                     {
                         <a class="page-title" href="@Url.Action("edit", "cmspage", new { id = item.Id, region = item.Template.DefaultRegionId })">@PageName(item)</a>
                     }
                 </text>
                );
            rows.Bound().RowItemTemplate(
                @<text>
                @if (item.PreviousSibling != null)
                {
                <a href="@Url.Action("move", "cmspage", new { id = item.Id, moveToId = item.PreviousSibling.Id })" class="b-icon b-icon_up"></a>
                }
		    	@if (item.NextSibling != null)
       {
                <a href="@Url.Action("move", "cmspage", new { id = item.Id, moveToId = item.NextSibling.Id })" class="b-icon b-icon_down"></a>
       }
                </text>
                ).HtmlAttributes(new { @class = "text-center" });
            rows.Bound().RowItemTemplate(
                @<text>
                <a class="page-preview b-icon b-icon_loupe" href="@Url.Action("Preview","CmsPage", new {id = item.Id})" target="_blank" rel="@item.Id">Link</a>
                </text>
                ).HtmlAttributes(new { @class = "text-center" });
            rows.Bound().RowItemTemplate(
                @<text>
                <span>@item.DatePublished.ToString("dd/MM/yyyy")</span>
                </text>
                );
            rows.Bound().RowItemTemplate(
                @<text>
                <span>@item.UserCreatedBy</span>
                </text>
                );
            rows.Bound().RowItemTemplate(
                @<text>
                     @if (item.Template != null && item.BehaviourPageType == CmsPageBehaviourTypeKind.Content)
                     {
                        <span>@item.Template.TemplateName</span>
                     }
                     else
                     {
                         <span>-</span>
                     }
                </text>
                );
            rows.Bound().RowItemTemplate(
                @<text>
                <span><a class="b-icon b-icon_copy" title="@_("Copy page")" href="@Url.Action("copy", "cmspage", new { id = item.Id })">@_("Copy page")</a></span>
                </text>
                );
            rows.Bound().RowItemTemplate(
                @<text>                                    
                    @if (item.ParentPageId.HasValue){  // we should not be able to delete Home page
                        <a class="b-icon b-icon_delete" title="@_("Delete page")" @(item.ChildrenCount > 0 ? new HtmlString("data-confirm-message=\""+_("The page has child pages, they will be deleted along the page. Proceed?")+"\"") : null) href="@Url.Action("delete", "cmspage", new { id = item.Id })">@_("Delete page")</a>
                    }
                </text>
                ).HtmlAttributes(new { @class = "t-last" }); 
        })
    .Render())